package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.domain.ResponseResult;
import com.example.domain.entity.Article;
import com.example.domain.entity.Product;
import com.example.exception.FloraException;
import com.example.service.CommentService;
import com.example.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/getProductPage")
    public ResponseResult getProductPage(
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Long created_user_id,
            @RequestParam(required = false) Long category_id,
            @RequestParam(required = false,defaultValue = "0") Integer pageNum,
            @RequestParam(required = false,defaultValue = "12") Integer pageSize
    ){
        Map<String,Object> condition = new HashMap<>();
        if(title!=null){
            condition.put("title",title);
        }
        if(created_user_id!=null){
            condition.put("created_user_id",created_user_id);
        }
        if(category_id!=null){
            condition.put("category_id",category_id);
        }

        return ResponseResult.okResult(productService.getProductPage(condition,pageNum,pageSize));
    }

    @PostMapping("/add")
    public Product add(@RequestBody Product product) throws FloraException {
        return productService.add(product);
    }

    @PutMapping("/update")
    public void updateProduct(@RequestBody Product product) throws FloraException {
        productService.update(product);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) throws FloraException {
        productService.delete(id);
    }
}